//
//  AppointmentPlan.m
//  ICrEAM
//
//  Created by Cty on 2016/11/07.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppointmentPlan.h"
#import "ICMAppointmentPlanModel.h"
#import "AppointmentPlanCell.h"
#import "ICMDatePickerView.h"
#import "NSDate+convenience.h"
#import "ICMTitleTextFeildView.h"
#import "PACACalendarView.h"
@interface AppointmentPlan()<UITableViewDelegate,UITableViewDataSource,ICMDatePickerViewDelegate,ICMAppointmentPlanCellDelegate,PACACalendarViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UITableView *tableViewToday;
@property (nonatomic, strong) NSDate *selectedDateFrom;
@property (nonatomic, strong) NSDate *selectedDateTo;
@property (nonatomic, strong) ICMTitleTextFeildView *labelFrom;
@property (nonatomic, strong) ICMTitleTextFeildView *labelTo;
@property (nonatomic,strong) PACACalendarView *pickerFrom;
@property (nonatomic,strong) PACACalendarView *pickerTo;
@property (nonatomic, assign) BOOL hasNext;
@property (nonatomic, assign) int pageNo;
@end
@implementation AppointmentPlan
+ (instancetype)creatViewWithFrame:(CGRect)frame andModels:(NSArray *)dataSource{
    
    
    return [[AppointmentPlan alloc]initWithFrame:frame andModels:dataSource];
}

- (instancetype)initWithFrame:(CGRect)frame andModels:(NSArray *)dataSource{
    
    self = [super initWithFrame:frame];
    
    if(self){
        //        self.dataSource = dataSource;
        [self setupViews];
        self.hasNext = YES;
        [self setRefresh];
    }
    
    return self;
}

- (void)setupViews{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(95))];
    
    self.labelFrom = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(ActualWidth(75), ActualHeight(10), ActualWidth(150), ActualHeight(75)) title:@"预约进厂时间" addTarget:self selector:@selector(clickCalenderBtnFrom)];
    
    
    UIButton *btnFrom = [[UIButton alloc]initWithFrame:CGRectMake(self.labelFrom.rightEdge+2, ActualHeight(40), ActualWidth(30), ActualWidth(30))];
    [btnFrom addTarget:self action:@selector(clickCalenderBtnFrom) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualHeight(30), ActualWidth(30))];
    imageView.image = [UIImage imageNamed:Appointment_Calendar];
    [btnFrom addSubview:imageView];
    
    UILabel *labelText = [[UILabel alloc]initWithFrame:CGRectMake(btnFrom.rightEdge+10, btnFrom.y, ActualWidth(30), ActualHeight(30))];
    
    labelText.text = @"至";
    labelText.textColor = Color(0x666666);
    
    self.labelTo = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(labelText.rightEdge+10, ActualHeight(10), ActualWidth(150), ActualHeight(75)) title:@"" addTarget:self selector:@selector(clickCalenderBtnTo)];
    
    UIButton *btnTo = [[UIButton alloc]initWithFrame:CGRectMake(self.labelTo.rightEdge+2, ActualWidth(40), ActualWidth(30), ActualWidth(30))];
    [btnTo addTarget:self action:@selector(clickCalenderBtnTo) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *imageViewTo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualHeight(30), ActualWidth(30))];
    imageViewTo.image = [UIImage imageNamed:Appointment_Calendar];
    
    self.labelTo.backgroundColor = Color(0xF4F5F7);
    
    [btnTo addSubview:imageViewTo];
    
    UIButton *btnSearch = [[UIButton alloc]initWithFrame:CGRectMake(btnTo.rightEdge+ActualWidth(30), ActualHeight(20), ActualWidth(50), ActualHeight(60))];
    
    [btnSearch addTarget:self action:@selector(searchByDate) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageViewSearch = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ActualWidth(30), ActualHeight(30))];
    imageViewSearch.image = [UIImage imageNamed:Appointment_SearchBtn];
    
    UILabel *labelSearch = [[UILabel alloc]initWithFrame:CGRectMake(0, imageViewSearch.bottomEdge, ActualWidth(50), ActualHeight(20))];
    labelSearch.text = @"查找";
    labelSearch.textColor = TitleColor;
    [btnSearch addSubview:imageViewSearch];
    [btnSearch addSubview:labelSearch];
    
    headerView.backgroundColor = Color(0xF1F3F3);
    [headerView addSubview:self.labelFrom];
    [headerView addSubview:btnFrom];
    [headerView addSubview:labelText];
    [headerView addSubview:self.labelTo];
    [headerView addSubview:btnTo];
    [headerView addSubview:btnSearch];
    
    [self addSubview:headerView];
    [self addSubview:self.tableViewToday];
}

-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

//上拉加载
- (void)setRefresh {
    __weak AppointmentPlan *my = self;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.hasNext) {
            self.pageNo++;
            [my loadMoreData];
        } else {
            [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
        }
    }];
    
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据了" forState:MJRefreshStateNoMoreData];
    self.tableViewToday.mj_footer = footer;
}

- (void)loadMoreData
{
    if([self.delegate respondsToSelector:@selector(loadMoreDataWithText:WithType:WithStartDate:WithEndDate:WithPage:)])
    {
        NSString *startDate = @"";
        NSString *endDate = @"";
        if (self.isSearchByDate) {
            startDate = [ICrEAMTool getStringWithDate:self.selectedDateFrom];
            endDate = [ICrEAMTool getStringWithDate:self.selectedDateTo];
        }
        [self.delegate loadMoreDataWithText:self.condition WithType:ICMEnumIntToString(ICMEnumAppointmentTypePlan) WithStartDate:startDate WithEndDate:endDate WithPage:self.pageNo];
    }
}

//加载更多数据
- (void)loadMoreDataWithData:(id)data {
    if ([data[@"CODE"] integerValue] == 200) {
        if (IsArr(data[@"TT_BOOKING_OEDER"])) {
            NSArray *dataArray = data[@"TT_BOOKING_OEDER"];
            if (dataArray.count != 0 && dataArray) {
                for (NSDictionary *searchData in dataArray) {
                    ICMAppointmentPlanModel * model = [ICMAppointmentPlanModel yy_modelWithDictionary:searchData];
                    [self.dataSource PACA_AddObject:model];
                }
                self.hasNext = YES;
                [self.tableViewToday.mj_footer endRefreshing];
            } else {
                self.hasNext = NO;
                [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
            }
        } else {
            self.hasNext = NO;
            [self.tableViewToday.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableViewToday reloadData];
    } else {
        [self.tableViewToday.mj_footer endRefreshing];
    }
}

/*tableViewDelegate tableViewDataSource*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AppointmentPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppointmentPlanCell"];
    if (!cell) {
        cell = [[AppointmentPlanCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppointmentPlanCell"];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateDataWithModel:[self.dataSource objectAtIndex:indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ActualHeight(100);
}

- (UITableView *)tableViewToday{
    if (!_tableViewToday) {
        _tableViewToday = [[UITableView alloc]initWithFrame:CGRectMake(0, ActualHeight(95)+1, self.frame.size.width, self.frame.size.height-ActualHeight(95))];
        _tableViewToday.delegate = self;
        _tableViewToday.dataSource = self;
        _tableViewToday.backgroundColor = BackgroundColor_White;
        [_tableViewToday registerClass:[AppointmentPlanCell class] forCellReuseIdentifier:@"AppointmentPlanCell"];
        _tableViewToday.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
        _tableViewToday.tableFooterView.backgroundColor = [UIColor whiteColor];
        _tableViewToday.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableViewToday;
}

- (void)clickCalenderBtnFrom{
    [Window addSubview:self.pickerFrom];
}
- (void)clickCalenderBtnTo{
    [Window addSubview:self.pickerTo];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ICMAppointmentPlanModel *model = self.dataSource[indexPath.row];
    if([model.CAN_EDIT isEqualToString:@"12781002"] && [ICrEAMTool isVW]){
        [self showString:@"微信的预约单，不允许编辑!" forSeconds:1.5];
        
        return;
    }

    if([self.delegate respondsToSelector:@selector(didClickPlanCellWithIndex:WithBookOrderNo:)]){
        [self.delegate didClickPlanCellWithIndex:indexPath WithBookOrderNo:model.BOOKING_ORDER_NO];
    }
    
}
/*ICMDatePickerViewDelegate*/

- (void)calendarView:(PACACalendarView *)calendarView didSelectedDate:(NSDate*)selectedDate{
    if(calendarView == self.pickerFrom){
        self.selectedDateFrom = selectedDate;
        NSString *dateString = [selectedDate separatorStyleDateTimeString];
        self.labelFrom.contentTextFeild.text = dateString;
    }else{
        self.selectedDateTo = selectedDate;
        NSString *dateString = [selectedDate separatorStyleDateTimeString];
        self.labelTo.contentTextFeild.text = dateString;
    }
}

//清空时间
- (void)delTime:(PACACalendarView *)calendarView
{
    if(calendarView == self.pickerFrom){
        self.selectedDateFrom = nil;
        self.labelFrom.contentTextFeild.text = @"";
    }else{
        self.selectedDateTo = nil;
        self.labelTo.contentTextFeild.text = @"";
        
    }
}

- (PACACalendarView *)pickerFrom{
    if(!_pickerFrom){
        _pickerFrom = [PACACalendarView createCalendarViewWithDidSelectedDate:nil minDate:[NSDate date] maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
        _pickerFrom.delegate = self;
        
    }
    return _pickerFrom;
}
- (PACACalendarView *)pickerTo{
    if(!_pickerTo){
        _pickerTo = [PACACalendarView createCalendarViewWithDidSelectedDate:nil minDate:[NSDate date] maxDate:nil andFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, ActualWidth(300), ActualWidth(380))];
        _pickerTo.delegate = self;
        
    }
    return _pickerTo;
}

- (void)reloadDataWithArray:(NSArray *)dataSource{
    [self.dataSource removeAllObjects];
    if (dataSource.count > 0) {
        self.pageNo = 1;
        self.hasNext = YES;
        [self setRefresh];
    } else {
        self.tableViewToday.mj_footer.hidden = YES;
        [self showString:@"暂无相关搜索结果" forSeconds:1.5];
    }
    [self.dataSource addObjectsFromArray:dataSource];
    [self.tableViewToday reloadData];
}

- (void)searchByDate{
    if ([self.delegate respondsToSelector:@selector(didSearchByStartDate:andEndDate:)]) {
        [self.delegate didSearchByStartDate:self.selectedDateFrom andEndDate:self.selectedDateTo];
    }
    
}
- (void)didClickPreViewWithIndex:(NSIndexPath *)index{
    ICMAppointmentPlanModel *model = self.dataSource[index.row];
    if ([self.delegate respondsToSelector:@selector(didClickPreViewWithIndex:WithBookOrderNo:)]) {
        [self.delegate didClickPreViewWithIndex:index WithBookOrderNo:model.BOOKING_ORDER_NO];
    }
    
}
@end
